package com.xx.news.viewmodel;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.xx.news.base.BaseViewModel;
import com.xx.news.entity.Toutiao;
import com.xx.news.repository.NewsRepository;

import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class ToutiaoViewModel extends BaseViewModel {
    NewsRepository repository;
    private MutableLiveData<Toutiao> Toutiaos = new MutableLiveData<>();

    public ToutiaoViewModel(@NonNull Application application) {
        super(application);
        setTitle("头条");
        repository = new NewsRepository(application);
    }

    public LiveData<Toutiao> getLiveData() {
        return Toutiaos;
    }

    public ObservableField<String> ButtonName1 = new ObservableField<String>();

    public void getDate() {
        getUC().getShowDialogEvent().call();
        repository.getData().subscribe(new Observer<Toutiao>() {
            @Override
            public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {
                Log.d("abcd", "onSubscribe");
            }

            @Override
            public void onNext(@io.reactivex.rxjava3.annotations.NonNull Toutiao toutiao) {
                Log.d("abcd", "onNext: " + toutiao.toString());
                getUC().getDismissDialogEvent().call();

                Toutiaos.postValue(toutiao);
            }

            @Override
            public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                Log.d("abcd", "onError");
            }

            @Override
            public void onComplete() {
                Log.d("abcd", "onComplete");
            }
        });
    }

}
